/*
 * Keyboard.c
 *
 * Created: 2012-07-09 19:14:28
 *  Author: Sebastian_2
 */ 

#include "Menus.h"

void Menu_Keyboard_Enter() {
	mKeyboardDisplay = UPPERCASE;
	
	Menu_Keyboard_Refresh();
}

void Menu_Keyboard_Refresh() {
	//gMenu = MENU_KEYBOARD;
	point p = {SSD1289_MaxX, 120};
	gColour = BACKGROUND;
	SSD1289_DrawRect(&(point){0, 0}, &p);
	
	if(mKeyboardDisplay == UPPERCASE) {
		p = (point){15, 68};
		ShowRGB565("/calendar/upper.565", &p);
		p =	(point){103, 194};
		ShowRGB565("/calendar/space.565", &p);
		p = (point){4, 159};
		ShowRGB565("/calendar/leftdown.565", &p);
		p = (point){281, 159};
		ShowRGB565("/calendar/right.565", &p);
	} else if(mKeyboardDisplay == LOWERCASE) {
		p = (point){31, 68};
		ShowRGB565("/calendar/lower.565", &p);
		p = (point){103, 194};
		ShowRGB565("/calendar/space.565", &p);
		p = (point){4, 159};
		ShowRGB565("/calendar/leftup.565", &p);
		p = (point){281, 159};
		ShowRGB565("/calendar/right.565", &p);
	} else if(mKeyboardDisplay == NUM) {
		p = (point){4, 159};
		gColour = BACKGROUND;
		SSD1289_DrawRect(&p, &(point){33, SSD1289_MaxY});
		p = (point){28, 68};
		ShowRGB565("/calendar/nums.565", &p);
		p = (point){103, 194};
		ShowRGB565("/calendar/space.565", &p);
		p = (point){9, 209};
		ShowRGB565("/calendar/text.565", &p);
		p = (point){281, 159};
		ShowRGB565("/calendar/right.565", &p);
	}
}

void Menu_Keyboard_AddNewChar(char c) {
	point p = {10, 10};
	uint8_t len, capacity;
	
	if(mCalendarEvent != EVENT_NAME && (c < 48 || c > 57)) // c between 0 and 9
		return;
	
	if(*mKeyboardString == 0 && mKeyboardDisplay == UPPERCASE) {
		mKeyboardDisplay = LOWERCASE;
		Menu_Keyboard_Refresh();
	}
	len = strlen(mKeyboardString);
	if(mCalendarEvent == EVENT_NAME)
		capacity = 10;
	else
		capacity = 2;
	
	if(len >= capacity)
		return; // end of string
	mKeyboardString[len] = c;
	mKeyboardString[len+1] = 0;
	if(c != ' ') {
		gColour = BACKGROUND;
		SSD1289_DrawRect(&p, &(point){SSD1289_MaxX, 50});
		gColour = FONT_COLOUR;
		SSD1289_PrintString(mKeyboardString, &p, &droidSans22ptFontInfo);
	}	
}

void Menu_Keyboard_RemoveLastChar() {
	point p = {10, 10};
	uint8_t len = strlen(mKeyboardString);
	len--;
	char lastChar = mKeyboardString[len];
	mKeyboardString[len] = 0;
	if(lastChar != ' ') {
		gColour = BACKGROUND;
		SSD1289_DrawRect(&p, &(point){SSD1289_MaxX, 50});
		gColour = FONT_COLOUR;
		SSD1289_PrintString(mKeyboardString, &p, &droidSans22ptFontInfo);
	}	
}